package com.gsq.controller;

import com.gsq.common.pojo.Admin;
import com.gsq.common.vo.PageResult;
import com.gsq.common.vo.SysResult;
import com.gsq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /*  Admin登陆    */
    @PostMapping("/login")
    public SysResult login(@RequestBody Admin admin){
        String token = adminService.login(admin);
        if (StringUtils.hasLength(token)){
            return SysResult.success(token);
        }
        return SysResult.fail();
    }
    /*  数据展现    */
    @GetMapping("/getAdminList")
    public SysResult getAdminList(PageResult pageResult){
        pageResult = adminService.getAdminList(pageResult);
        return SysResult.success(pageResult);
    }
    /*  用户状态修改  */
    @PutMapping("/adminValid/{id}/{valid}")
    public SysResult updatedValid(Admin admin){
        adminService.updatedValid(admin);
        return SysResult.success();
    }
    /* 添加用户 */
    @PostMapping("/addAdmin")
    public SysResult addAdmin(@RequestBody Admin admin){
        adminService.addAdmin(admin);
        return SysResult.success();
    }
    /* 修改用户信息 */
    @PutMapping("/updateAdmin")
    public SysResult updateAdmin(@RequestBody Admin admin){
        adminService.updateAdmin(admin);
        return SysResult.success();
    }
    /* 删除用户 */
    @DeleteMapping("/{id}")
    public SysResult deleteAdmin(@PathVariable Integer id){
        adminService.delelteAdmin(id);
        return SysResult.success();
    }
    /* 根据id反馈用户信息*/
    @GetMapping("/{id}")
    public SysResult returnAdmin(@PathVariable Integer id){
        Admin admin = adminService.retrunAdmin(id);
        return SysResult.success(admin);
    }
}
